package com.nuanshui.heatedloan.userinfo.service.thirdLogin;

import com.nuanshui.framework.consts.TransactionManagerType;
import com.nuanshui.heatedloan.userinfo.facade.ThirdAuthService;
import com.nuanshui.heatedloan.userinfo.facade.UserService;
import com.nuanshui.heatedloan.userinfo.facade.UserStateService;
import com.nuanshui.heatedloan.userinfo.facade.thirdlogin.ThirdUserService;
import com.nuanshui.heatedloan.userinfo.facade.thirdlogin.ThirdUserServiceFactory;
import com.nuanshui.heatedloan.userinfo.form.BoundUserForm;
import com.nuanshui.heatedloan.userinfo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by zxh on 2017/2/10.
 */
@Service("ThirdAuthService")
@Transactional(readOnly=true,value= TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
public class ThirdAuthServiceImpl implements ThirdAuthService {

    @Autowired
    @Qualifier("ThirdUserServiceFactory")
    ThirdUserServiceFactory thirdUserServiceFactory;

    @Autowired
    private UserService userService;

    @Autowired
    private UserStateService userStateService;

    @Transactional(readOnly=false,value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
    public User thirdCheckUser(BoundUserForm boundUserForm)
    {
        String channel=boundUserForm.getChannel();

        ThirdUserService thirdUserService=thirdUserServiceFactory.GetThirdUserService(boundUserForm.getAccountType());

        User user = null;
        if(thirdUserService!=null)
        {
            user=thirdUserService.authUser(boundUserForm);
        }

        if (user.getIsNew()) {
            userStateService.setUserChannel(user.getId(), channel);
        }

        return user;
    }
}
